package com.wg.dome.attribute;

import com.wg.dome.GameConstant;
import com.wg.dome.entity.EquipType;
import com.wg.dome.entity.config.*;
import com.wg.dome.entity.user.EquipEntity;
import com.wg.dome.entity.user.HeroEntity;
import com.wg.dome.repository.*;

import java.io.Serializable;
import java.util.HashMap;

/**
 * 属性封装
 */
public class AttributeHelper {
    /**
     * 计算英雄属性
     */
    public static Attribute countHeroAttribute(HeroEntity heroEntity) {
        Attribute attribute = new Attribute();
        InitHeroRepository initHeroRepository = new InitHeroRepository();
        InitHeroEntity initHeroEntity = initHeroRepository.get(heroEntity.getsId());
        if (initHeroEntity == null) {
            return attribute;
        }

        attribute.set(AttributeType.HP, initHeroEntity.getHp());
        attribute.set(AttributeType.AMP, initHeroEntity.getAmp());
        attribute.set(AttributeType.ATK, initHeroEntity.getAtk());
        attribute.set(AttributeType.CRT, initHeroEntity.getCrt());
        attribute.set(AttributeType.DAMAGE_RES, initHeroEntity.getDamageRes());
        attribute.set(AttributeType.DEF, initHeroEntity.getDef());
        attribute.set(AttributeType.DEX, initHeroEntity.getDex());
        attribute.set(AttributeType.ENERGY, initHeroEntity.getEnergy());
        attribute.set(AttributeType.EVD, initHeroEntity.getEvd());
        attribute.set(AttributeType.KBL, initHeroEntity.getKbl());
        attribute.set(AttributeType.RGS, initHeroEntity.getRgs());
        attribute.set(AttributeType.SKILL_RES, initHeroEntity.getSkillRes());
        attribute.set(AttributeType.SKILL_UP, initHeroEntity.getSkillUp());
        return attribute;
    }

    /**
     * 计算装备属性
     */
    public static Attribute countEquipAttribute(EquipEntity equipEntity) {
        Attribute attribute = new Attribute();
        if (equipEntity == null) {
            return attribute;
        }

        InitEquipRepository initEquipRepository = new InitEquipRepository();
        InitEquipEntity initEquipEntity = initEquipRepository.get(equipEntity.getsId());
        if (initEquipEntity == null) {
            return attribute;
        }

        EquipType equipType = initEquipEntity.getEquipType();

        int value = initEquipEntity.getValue();

        //强化
        EquipQiangHuaRepository qiangHuaRepository = new EquipQiangHuaRepository();
        value += qiangHuaRepository.getValue(equipEntity.getQhLv(), equipType);

        //精炼
        EquipJingLianRepository jingLianRepository = new EquipJingLianRepository();
        value += jingLianRepository.getValue(equipEntity.getJlLv(), equipType);


        AttributeType attributeType = initEquipEntity.getAttributeType();
        attribute.set(attributeType, value);

        //注灵
        if (equipType == EquipType.Weapon) {
            ZhuLingWeaponRepository zhuLingWeaponRepository = new ZhuLingWeaponRepository();
            zhuLingWeaponRepository.getAttribute(equipEntity.getZlLv(), attribute);
        } else if (equipType == EquipType.Clothes) {
            ZhuLingClothesRepository zhuLingClothesRepository = new ZhuLingClothesRepository();
            zhuLingClothesRepository.getAttribute(equipEntity.getZlLv(), attribute);
        } else if (equipType == EquipType.Ring) {
            ZhuLingRingRepository zhuLingRingRepository = new ZhuLingRingRepository();
            zhuLingRingRepository.getAttribute(equipEntity.getZlLv(), attribute);
        } else {
            ZhuLingShoeRepository zhuLingShoeRepository = new ZhuLingShoeRepository();
            zhuLingShoeRepository.getAttribute(equipEntity.getZlLv(), attribute);
        }

        //灵石
        EquipLingShiRepository equipLingShiRepository = new EquipLingShiRepository();
        equipLingShiRepository.getAttribute(equipEntity.getLs(), attribute);

        return attribute;
    }

    public static Attribute[] getFightAttributes(long playerId) {
        EquipRepository equipRepository = new EquipRepository();
        HeroRepository heroRepository = new HeroRepository();
        Attribute[] attributes = new Attribute[GameConstant.FIGHT_HERO_NUMBER];
        for (int i = 0; i < attributes.length; i++) {
            byte pos = (byte) (i + 1);
            attributes[i] = getSignHeroAttribute(playerId, pos);
        }
        return attributes;
    }

    public static Attribute getSignHeroAttribute(long playerId, int pos) {
        EquipRepository equipRepository = new EquipRepository();
        HeroRepository heroRepository = new HeroRepository();
        Attribute totalAttributes = new Attribute();
        HeroEntity heroEntity = heroRepository.get(playerId, entity -> entity.getSzPos() == pos);
        if (heroEntity != null) {
            totalAttributes.addAll(heroEntity.getAttribute());
        }

        HashMap<Serializable, EquipEntity> equipMap = equipRepository.getAllByGroupId(playerId, entity -> entity.getPos() == pos);
        for (EquipEntity equipEntity : equipMap.values()) {
            totalAttributes.addAll(equipEntity.getAttribute());
        }
        return totalAttributes;
    }
}
